package com.dongdong.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.dongdong.em.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.model.TbUser;
import com.dongdong.service.SeckillGoodsService;
import com.dongdong.util.JWTUtil;
import com.dongdong.vo.SeckillGoodsVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("seckillgoods")
public class SeckillGoodsController {

    @Autowired
    private SeckillGoodsService seckillGoodsService;

    @Autowired
    private JWTUtil jwtUtil;


    //秒杀商品预热(定时器每隔一段时间 预热一次)
    @GetMapping("redisHot")
    @Scheduled(cron = "0 0/10 * * * ?")      //每隔10分钟执行一次     0 */5 * * * ?
    public Result redisHot() {
        seckillGoodsService.redisHot();
        System.out.println("秒杀商品预热好了....");
        return Result.OK();
    }

    //查询缓存的秒杀商品信息
    @GetMapping("getSeckillGoods")
    @SentinelResource(value = "getSeckillGoods", blockHandler = "blockMethed")//前面那个随便写  后面的必须对应上
    public Result getSeckillGoods() {
        List<SeckillGoodsVO> seckillGoodsVOList = seckillGoodsService.getSeckillGoods();
        return Result.OK().setData(seckillGoodsVOList);
    }

    //编写降级方法
    public Result blockMethed(BlockException exception) {
        return Result.ZHRESPONSE(ResultEnum.SECKILL_QUERY_ERROR);//秒杀查询点击次数过多
    }


    //-------------------------------------------------测试sentinel 熔断降级---------------------------------------------
    //-------------------------------------------------关联---------------------------------------------

    //查询订单
    @GetMapping("getOrder")
    public Result getOrder() {
        return Result.OK().setData("查询订单");
    }

    //修改订单
    @GetMapping("UpOrder")
    public Result UpOrder() {
        return Result.OK().setData("修改订单");
    }


    //--------------------------------------------------链路---------------------------------------


    //save
    @GetMapping("save")
    public Result save() {
        seckillGoodsService.select();
        return Result.OK();
    }

    //query
    @GetMapping("query")
    public Result query() {
        seckillGoodsService.select();
        return Result.OK();
    }

    //根据商品id  查询商品信息 以及所属商家
    @GetMapping("selectById")
    public Result selectById(@RequestParam Long id) {
        SeckillGoodsVO vo = seckillGoodsService.selectById(id);
        return Result.OK().setData(vo);
    }

    //-------------------------------------------------测试sentinel 熔断降级---------------------------------------------
    //-------------------------------------------------测试sentinel 熔断降级---------------------------------------------


    //根据id查询redis中的秒杀商品详情
    @PostMapping("selectBySeckillId")
    public Result selectBySeckillId(@RequestParam Long sekillGId) {
        SeckillGoodsVO vo = seckillGoodsService.selectBySeckillId(sekillGId);
        return Result.OK().setData(vo);
    }


    //秒杀抢购订单提交
    @PostMapping("SeckillSubOrder")
    public Result SeckillSubOrder(@RequestHeader String Autherizetion, @RequestParam Long sekillGId) {
        //1.解析token  获取用户id
        Claims claims = jwtUtil.parseJwt(Autherizetion);
        if (claims == null) {
            return Result.ZHRESPONSE(ResultEnum.USER_NULL);//用户未登录
        }
//        String userid = claims.getId();

        //获取整个user对象
        String subject = claims.getSubject();
        TbUser user = JSON.parseObject(subject, TbUser.class);

        //2.拿到用户id  和   秒杀id  传到实现类
        Result result = seckillGoodsService.SeckillSubOrder(user.getUsername(), sekillGId);
        return result;
    }
}
